package com.base.cn.platform.os.service.op.affiche.tmp;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.msg.WXTmpEnum;
import com.base.cn.platform.os.entity.user.user.CusUser;
import com.base.cn.platform.os.service.manage.config.SysConfigBiz;
import com.base.cn.platform.os.service.op.affiche.OpReceiveAfficheBiz;
import com.base.cn.platform.os.service.user.user.CusUserBiz;
import com.base.cn.platform.os.service.user.user.CuserProfileBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 发送消息
 *
 * @author s.li
 * @date 2019/05/28
 */
@Component
@Slf4j
public class SendMessage {
    @Autowired
    private RegSendTemAffice regSendTemAffice;
    @Autowired
    private OrderSendTemAffice orderSendTemAffice;
    @Autowired
    private LiveSendTemAffice liveSendTemAffice;
    @Autowired
    private ExamSendTemAffice examSendTemAffice;
    @Autowired
    private AgentSendTemAffice agentSendTemAffice;
    @Autowired
    private PushTaskSendTemAffice pushTaskSendTemAffice;
    @Autowired
    private QaReplySendTemAffice qaReplySendTemAffice;
    @Autowired
    private FeedbackSendTemAffice feedbackSendTemAffice;
    @Autowired
    private CouponCodeSendTemAffice couponCodeSendTemAffice;
    @Autowired
    private RefundSendTemAffice refundSendTemAffice;
    @Autowired
    private CashSendTemAffice cashSendTemAffice;
    @Autowired
    private OpReceiveAfficheBiz receiveAfficheBiz;
    @Autowired
    private SysConfigBiz sysConfigBiz;
    @Autowired
    private CuserProfileBiz cuserProfileBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;
    @Autowired
    private CusUserBiz cusUserBiz;

    /**
     * 发送消息
     * @param tmpEnum 消息类型
     * @param userIdList 用户ID列表
     * @param dataListMap 业务数据
     */
    @Async
    public void sendMessage(WXTmpEnum tmpEnum, List<BigDecimal> userIdList, Map<BigDecimal,List> dataListMap){
        try{
            if(tmpEnum != null){
                //查询消息模板内容
                TreeMap<String, String> configMap = sysConfigBiz.getSysConfigContext(ConfigName.SMST);
                //查询用户数据列表
                String userIds = userIdList.stream().map(e->e.toString()).collect(Collectors.joining(","));
                List<CusUser> userList = cusUserBiz.queryUserByIds(userIds,false,false,false,false,false,false,false);
                Object[] arr = {receiveAfficheBiz,sysConfigBiz,cuserProfileBiz,customCacheChannel,configMap};
                //根据不同类型，发送不同消息
                if(tmpEnum.getKey().equals(WXTmpEnum.REGISTER.getKey())){//注册成功
                    regSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.ORDER.getKey())){//订单支付成功
                    orderSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.LIVE.getKey())){//支付开始前的30分钟提醒
                    liveSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.EXAM.getKey()) || tmpEnum.getKey().equals(WXTmpEnum.MARKING.getKey())){//完成考试,试卷完成试卷批阅
                    examSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.AGENT_SUCCESS.getKey()) || tmpEnum.getKey().equals(WXTmpEnum.AGENT_FAIL.getKey())){// 申请推广员成功消息 ,申请推广员未通过消息
                    agentSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.PUSH_TASK.getKey())){//发布作业
                    pushTaskSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.QA_REPLY.getKey())){//问答已回复
                    qaReplySendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.FEEDBACK.getKey())){//意见反馈消息
                    feedbackSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.COUPON_CODE.getKey())){// 优惠券消息
                    couponCodeSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.REFUND.getKey())){// 退费账户消息
                    refundSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }else if(tmpEnum.getKey().equals(WXTmpEnum.CASH.getKey())){// 提现账户消息
                    cashSendTemAffice.sendMessage(arr,tmpEnum,userList,dataListMap);
                }
            }
        }catch (Exception e){
            log.error("sendMessage()--error",e);
        }
    }

    /**
     * 发送消息
     * @param key 消息类型
     * @param userIdList 用户ID列表
     * @param dataListMap 业务数据
     */
    @Async
    public void sendMessage(String key, List<BigDecimal> userIdList, Map<BigDecimal,List> dataListMap){
        WXTmpEnum tmpEnum = WXTmpEnum.getWXTmpEnum(key);
        this.sendMessage(tmpEnum,userIdList,dataListMap);
    }
}
